<?php
namespace Model;

class Auth 
{
	public function common($node)
	{
	    $result = array('valid'=>false,'errorCode'=>'00000','result'=>'');
	    
	    $userID = getIdenty();
		if($userID ==1){
			$result['valid'] = true;
			return $result;
		}
	    if($userID == false)
	    {
	        $result['errorCode'] = '00001';
	        $result['result'] = $this->errorCode($result['errorCode']);
	        return $result;
	    }
	    
	    if(empty($node) == true)
	    {
	        $result['errorCode'] = '00002';
	        $result['result'] = $this->errorCode($result['errorCode']);
	        return $result;
	    }

	    $nodeID = MOA('permission_node')->where(array('node'=>$node,'status'=>1))->getField('id');
	    if(empty($nodeID) == true)
	    {
	        $result['errorCode'] = '00003';
	        $result['result'] = $this->errorCode($result['errorCode']);
	        return $result;
	    }
		
	    $rule = MOA('permission_rule')->where(array('userID'=>getIdenty()))->getField('rule');
	    if(empty($rule) == true)
	    {
	        $result['errorCode'] = '00004';
	        $result['result'] = $this->errorCode($result['errorCode']);
	        return $result;
	    }

	    $ruleArray = explode(',',$rule);
	    if(in_array($nodeID,$ruleArray) == false)
	    {
	        $result['errorCode'] = '00005';
	        $result['result'] = $this->errorCode($result['errorCode']);
	        return $result;
	    }

	    $result['valid'] = true;
	    $result['result'] = $this->errorCode($result['errorCode']);
	    return $result;
	}
	
	function errorCode($code)
	{
	    $result = '';
	    switch($code)
	    {
	        case '00000':
	            $result = '验证通过';
	            break;
	        case '00001':
	            $result = '尚未登录';
	            break;
            case '00002':
                $result = '验证节点为空';
                break;
            case '00003':
                $result = '验证节点不存在';
                break;
            case '00004':
                $result = '用户节点不存在';
                break;
            case '00005':
                $result = '未授权访问';
                break;
	    }
	    
	    return $result;
	}
}
